package edu.mit.media.fv.components
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.*;
	
	public class FVFlickButton extends Sprite
	{
		private var _field:TextField;
		private var _width:int;
		private var _height:int;
		private var _format:TextFormat;
		private var _flickedLeft:Boolean = false;
		private var _downPoint:int = 0;//x coordinate of the last mouseDown point

		public function FVFlickButton(x:int = 0, y:int = 0, width:int = 100, height:int = 28, text:String = ""){
			super();
			
			_format = new TextFormat();
			_format.align = TextFormatAlign.CENTER;
			_format.bold = true;
			_format.font = "Arial";
			_format.size = height*0.8;
			_format.color = 0x000000;
			
			this._field = new TextField();
			this._field.defaultTextFormat = _format;
			this._field.type = TextFieldType.DYNAMIC;
			this._field.selectable = false;
			this.addChild(this._field);
			this.setBounds(x, y, width, height);
			this.text = text;
			
			this.addEventListener(MouseEvent.MOUSE_UP, processMouseUp);
			this.addEventListener(MouseEvent.MOUSE_DOWN, processMouseDown);
		}
		
		private function processMouseUp(event:MouseEvent):void{
			if(this._downPoint == 0){
				//do nothing
			}else if (Math.abs(event.localX-this._downPoint)<4){
				this.flickLeft(!this._flickedLeft);
			}else if(event.localX-this._downPoint > this._width/4){
				this.flickLeft(false);
			}else if(event.localX-this._downPoint < -this._width/4){
				this.flickLeft(true);
			}
			this._downPoint = 0;
		}
		private function processMouseDown(event:MouseEvent):void{
			this._downPoint = event.localX;
		}
		
		private function flickLeft(left:Boolean):void{
			this._flickedLeft = left;
			if(left){
				this.dispatchEvent(new FVChangeEvent(FVChangeEvent.VALUE_CHANGED, -1));
			}else{
				this.dispatchEvent(new FVChangeEvent(FVChangeEvent.VALUE_CHANGED, 1));
			}
		}
		
		//getters and setters
		public function setBounds(x:int, y:int, width:int, height:int):void{
			this._width = width;
			this._height = height;
			this.x = x;
			this.y = y;
			this._format.size = this._height*0.8;
			this._field.defaultTextFormat = this._format;
			this._field.width = this._width;
			this._field.height = this._height;
			this.draw();
		}
		public function get text():String{
			return this._field.text;
		}
		public function set text(text:String):void{
			this._field.text = text;
		}
		
		//Modifiers
		
		private function draw():void{
			var type:String = GradientType.LINEAR;
			var colors:Array = [0x3C3C3C, 0x737373];
			var alphas:Array = [1, 1];
			var ratios:Array = [0, 255];
			var matrix:Matrix = new Matrix();
			var boxWidth:Number = this._width;
			var boxHeight:Number = this._height;
			var boxRotation:Number = Math.PI/2; // 90˚
			var tx:Number = 0;
			var ty:Number = 0;
			matrix.createGradientBox(boxWidth, boxHeight, boxRotation, tx, ty);
			
			this.graphics.clear();
			this.graphics.lineStyle(1, 0xFFFFFF, 0.20, true);
			this.graphics.beginGradientFill(type, colors, alphas, ratios, matrix);
			this.graphics.drawRoundRect(0,0,this._width, this._height, 10,10);
			this.graphics.endFill();
		}

	}
}